#ifndef LOGGERMANAGER_H
#define LOGGERMANAGER_H

#include <QObject>
#include <QTextEdit>
#include <QPlainTextEdit>
#include <QList>
#include "appenderfactory.h"

class LoggerManager : public QObject
{
    Q_OBJECT
    Q_DISABLE_COPY_MOVE(LoggerManager)
public:
    explicit LoggerManager(QObject *parent = nullptr);

    static LoggerManager* instance();

    void startLogSystem();
    void stopLogSystem();

    //初始化命令行日志
    void initConsoleAppender(const QString& appenderName="console",
                             LogLevel level=LogLevel::Debug);
    //初始化文件日志
    void initFileAppender(const QString& logPath="./logs",
                          const QString& logName="app",
                          const QString& appenderName="file",
                          LogLevel level=LogLevel::Info);
    //初始化QTextEdit日志
    void initQTextEditAppender(QTextEdit* textEdit,
                               const QString& appenderName="qTextEdit",
                               LogLevel level=LogLevel::Debug);
    //初始化QPlainTextEdit日志
    void initQPlainTextEditAppender(QPlainTextEdit* plainTextEdit,
                                    const QString& appenderName="qPlainTextEdit",
                                    LogLevel level=LogLevel::Debug);
    //初始化TCP日志
    void initTcpAppender(const QString& ip="127.0.0.1",
                         quint16 port= 9009,
                         const QString& appenderName="tcp",
                         LogLevel level=LogLevel::Error);
    //初始化UDP日志
    void initUdpAppender(const QString& ip="127.0.0.1",
                         quint16 port= 9010,
                         const QString& appenderName="udp",
                         LogLevel level=LogLevel::Error);

    QList<QString> getAllAppenders();                       //获取所有添加的Appender
    QList<QString> getActiveAppenders();                    //获取所有激活的Appender
    QList<AppenderType> getAllAppenderType();               //获取所有添加的枚举
    QList<AppenderType> getActiveAppenderType();            //获取所有激活的枚举

    void setEnable(AppenderType type,bool enable);          //设置对应枚举的Appender使能
    void setEnable(const QString& appenderName,bool enable);//设置对应名称的Appender使能
    void setAllEnable(bool enable);                         //将当前已添加的Appender全部使能,只有手动添加的Appender才会被记录


    template <typename T>
    T* getActiveAppender(AppenderType type) {
        return AppenderFactory::instance()->getActiveAppender<T>(type);
    }

    // 获取Appender实例（用于自定义配置，如颜色）
    template <typename T>
    T* getAppender(const QString& name) {
        // 显式指定模板参数 T
        return AppenderFactory::instance()->getActiveAppender<T>(name);
    }
private:
    bool isIPv4Address(const QString& ipStr);


private:
    static LoggerManager* m_instance;
};

#endif // LOGGERMANAGER_H
